using System;
using System.Collections.Generic;
using GameFramework;
using UnityEngine;

namespace StarForce
{
    public class WatermelonManager
    {
        private Dictionary<WatermelonType, int> m_WatermelonEntityIds;

        private List<WatermelonType> m_WatermelonTypes;

        private static WatermelonManager m_Instance;

        private List<float> m_Random;

        private WatermelonGame m_WatermelonGame;
        public static WatermelonManager Instance
        {
            get
            {
                if (m_Instance == null)
                {
                    m_Instance = new WatermelonManager();
                }

                return m_Instance;
            }
        }


        private WatermelonManager()
        {
            m_WatermelonEntityIds = new Dictionary<WatermelonType, int>()
            {
                {WatermelonType.Yellow,1001},
                {WatermelonType.Blue,1002},
                {WatermelonType.Pink,1003},
                {WatermelonType.Green,1004},
                {WatermelonType.Red,1005},
            };   
            
            m_WatermelonTypes = new List<WatermelonType>()
            {
                WatermelonType.Yellow,WatermelonType.Blue,WatermelonType.Pink,WatermelonType.Green,WatermelonType.Red
            };
            
//            m_Random = new List<float>()
//            {
//                0.6f,
//                0.3f,
//                0.1f,
//            };
        }

        public void SetWatermelonGame(WatermelonGame watermelonGame)
        {
            m_WatermelonGame = watermelonGame;
        }

        /// <summary>
        /// 生成一个随机的西瓜
        /// </summary>
        public int CreateRandomWaterMelon(float x,bool isCompose)
        {
            WatermelonType type = m_WatermelonTypes[Utility.Random.GetRandom(0, m_WatermelonTypes.Count-2)];
            
//            float random = (float) Utility.Random.GetRandomDouble();
//
//            int index = 0;
//
//            float sum = 0;
//
//            for (int i = 0; i < m_Random.Count; i++)
//            {
//                if (i==0 && i<m_Random.Count)
//                {
//                    if ()
//                    {
//                        
//                    }
//                }
//            }
            
//            while (sum<random && random< sum+m_Random[index])
//            {
//                
//            }

            int entityId = m_WatermelonEntityIds[type];

            int id = GameEntry.Entity.GenerateSerialId();
            
            WatermelonData data = new WatermelonData(id,entityId,type,isCompose,new Vector3(x,4.5f,10));
            
            GameEntry.Entity.ShowWatermelon(data);

            m_WatermelonGame.CurWatermelonId = id;
            return id;
        }

        /// <summary>
        /// 生成指定类型的西瓜
        /// </summary>
        /// <param name="type"></param>
        public void CreateWaterMenlon(WatermelonType type,Vector3 position)
        {
            WatermelonData data = new WatermelonData(GameEntry.Entity.GenerateSerialId(),m_WatermelonEntityIds[type],type,true,position);
            
            GameEntry.Entity.ShowWatermelon(data);
        }


        /// <summary>
        /// 检查是否可以合成
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        public void Check(Watermelon a,Watermelon b)
        {
            if (a.Type != b.Type)
            {
                return;
            }
            int index = m_WatermelonTypes.IndexOf(a.Type);

            if (index < m_WatermelonTypes.Count-1)
            {
                index++;
                
                WatermelonType lastType = m_WatermelonTypes[index];

                if (GameEntry.Entity.HasEntity(a.Id) || GameEntry.Entity.IsLoadingEntity(a.Id))
                {
                    GameEntry.Entity.HideEntity(a);
                }

                if (GameEntry.Entity.HasEntity(b.Id) || GameEntry.Entity.IsLoadingEntity(b.Id))
                {
                    GameEntry.Entity.HideEntity(b);
                }
                
                CreateWaterMenlon(lastType,a.transform.localPosition);
            }

        }

    }
}